package com.aqie.medium.graph;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 841 钥匙和房间  经典 DFS
 */
public class CanVisitAllRooms {
    /**
     * 1, 2ms
     * @param rooms
     * @return
     */
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        boolean[] visited = new boolean[rooms.size()];
        int count = 1;
        visited[0] = true;
        Queue<Integer> queue = new LinkedList<>(rooms.get(0));

        while(!queue.isEmpty()){
            int room = queue.poll();
            if(!visited[room]){
                for(Integer i : rooms.get(room)){
                    queue.offer(i);
                }
                count++;
                visited[room] = true;
            }
        }
        return count == rooms.size();
    }
}
